package com.nCoV.EpidemicSurveillance.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.nCoV.EpidemicSurveillance.domain.PlaceNodes;
import com.nCoV.EpidemicSurveillance.domain.StudentNodes;
import com.nCoV.EpidemicSurveillance.domain.StudentPlaceRelationship;
import com.nCoV.EpidemicSurveillance.repository.PlaceRespository;
import com.nCoV.EpidemicSurveillance.repository.StuPlaRelsRespository;
import com.nCoV.EpidemicSurveillance.repository.StudentRespository;

/**
 * @author 张浩田
 * @date 2020年3月10日
 */
@Service
@Transactional
public class GraphService {

	private static final Logger logger = LogManager.getLogger(GraphService.class);
	
	@Autowired
	private StudentRespository studentRespository;
	
	@Autowired
	private PlaceRespository placeRespository;
	
	@Autowired
	private StuPlaRelsRespository stuplarelsRespository;
	
	/**
	 * 获取节点nodes
	 * 
	 * @return
	 */
	public List<Map<String, Object>> getNodes() {
		List<Map<String, Object>> nodes = new ArrayList<Map<String,Object>>();
		Map<String, Object> tempmap1 = null;
		Map<String, Object> tempmap2 = null;
		List<Map<String, Object>> templist = new ArrayList<Map<String,Object>>();
		try {
			List<StudentNodes> slist = studentRespository.getNodes();
			List<PlaceNodes> plist = placeRespository.getNodes();
			for(StudentNodes s : slist){
				tempmap1 = new HashMap<String, Object>();
				tempmap1.put("id", s.getId());
				tempmap1.put("label", s.getName());
				templist.add(tempmap1);
			}
			for(PlaceNodes s : plist){
				tempmap2 = new HashMap<String, Object>();
				tempmap2.put("id", s.getId());
				tempmap2.put("label", s.getName());
				templist.add(tempmap2);
			}
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			logger.info(e);
			tempmap1.put("code", "error");
			templist.add(tempmap1);
		}
		nodes.addAll(templist);
		return nodes;
	}

	/**
	 * 获取关系
	 */
	public List<Map<String, Object>> getEdges() {
		List<Map<String, Object>> edges = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> templist = new ArrayList<Map<String,Object>>();
		Map<String, Object> tempmap = null;
		try {
			List<StudentNodes> slist = studentRespository.getNodes();
			for(StudentNodes s : slist){
				List<StudentPlaceRelationship> list1 = stuplarelsRespository.getEdges(s);
				for(StudentPlaceRelationship spr : list1){
					tempmap = new HashMap<String, Object>();
					tempmap.put("from", spr.getStartNode().getId());
					tempmap.put("to", spr.getEndNode().getId());
					tempmap.put("label", spr.getName());
					templist.add(tempmap);
				}
			}
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			logger.info(e);
			tempmap.put("code", "error");
			templist.add(tempmap);
		}
		edges.addAll(templist);
		return edges;
	}
}
